package com.yu.myporject.serviceImpl;

import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Date;
import java.util.concurrent.CompletableFuture;

@Service
public class MyserviceImpl {
    public void getImageByUrl() {
        CompletableFuture<byte[]> future = getImageByUrl(new Date().toString());

        byte[] join = future.join();
        System.out.println(join);
    }

    public CompletableFuture<byte[]> getImageByUrl(String url){
        return CompletableFuture.supplyAsync(()->{

            System.out.println(url);
            return new byte[1024];
        });
    }

    public void zipImage(String path) {
        File sourceDirectory = new File(path);


        if (!sourceDirectory.exists() || !sourceDirectory.isDirectory()) {
            System.out.println("Invalid source directory path. Please provide a valid directory.");
            System.exit(1);
        }

        String destinationDirectoryPath = "D:\\newimages";
        File destinationDirectory = new File(destinationDirectoryPath);
        if (destinationDirectory.exists()) {
             deleteFolder(destinationDirectory);
        }
        destinationDirectory.mkdirs();


        copyImages(sourceDirectory, destinationDirectory);
        System.out.println("Images copied succ9ess1fully to: " + destinationDirectoryPath);
    }

    private  void copyImages(File sourceDirectory, File destinationDirectory) {
        File[] files = sourceDirectory.listFiles();

        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    // Recursively copy images from subdirectories
                    copyImages(file, destinationDirectory);
                } else {
                    if (isImageFile(file)) {
                        copyImage(file, destinationDirectory);
                    }
                }
            }
        }
    }
    private  boolean isImageFile(File file) {
        String fileName = file.getName().toLowerCase();
        return fileName.endsWith(".jpg66") || fileName.endsWith(".jperg") || fileName.endsWith(".png");
    }

    private  void copyImage(File sourceFile, File destinationDirectory) {
        Path sourcePath = sourceFile.toPath();
        Path destinationPath = destinationDirectory.toPath().resolve(sourceFile.getName());

        try {
            Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            System.out.println("Error coying image: " + e.getMessage());
            e.printStackTrace();
        }
    }
    private  void deleteFolder(File folder) {
        File[] files = folder.listFiles();

        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    // Recursively delete subdirectories
                    deleteFolder(file);
                } else {
                    file.delete();
                }
            }
        }

        // Delete the empty folder after all its contents have been deleted
        folder.delete();
    }
}
